<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Dangle
 *
 * @author sunday add 2014.5.28  modify by dragon.hu @2016.2.27
 */
require_once 'BaseChannel.php';

//当乐平台
class Dangle implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="gKrto20JQsRu"; // 12位支付密钥,当乐分配
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		$orderLog = new ChargeOrder();
		$orderLog->log("dangle", $_REQUEST);
		$result=$_REQUEST["result"];
		$money=$_REQUEST["money"];
		$orderNo=$_REQUEST["order"];
		$memberId=$_REQUEST["mid"];
		$dateTime=$_REQUEST["time"];
		$signature=$_REQUEST["signature"];
		$ext=$_REQUEST["ext"];
		$str="order=".$orderNo."&money=".$money."&mid=".$memberId."&time=".$dateTime."&result=".$result."&ext=".$ext."&key=".$paymentKey;
		$sig=strtolower(md5($str));
		
		if(result != null && $sig == $signature) 
		//if(result != null ) //for test 
		{ //验证通过
			 
			//$orderLog->log("dangle",array("order"=>$orderNo,"money"=>$money,"mid"=>$memberId,"time"=>$dateTime,
	  		//			"result"=>$result,"ext"=>$ext,"key"=>$this->paymentKey));
	  		
			//echo "\r\n status:".STATUS_SUCCESS." money:$money orderNo:$orderNo result:$result \r\n";
			if("1" == $result)
			{
				//进行发货
				if(false == $orderLog->recordOrder(STATUS_SUCCESS, $money, $ext, "dangle", $platform,$orderNo))
				{
					$ret="failed";
					$orderLog->log("dangle",array("order"=>$ext,"money"=>$money,"result"=>$result,"serverresult"=>"faild"));
					//echo " insert order:$ext error ";
				}else 
				{
					//支付成功
					$ret="success";
					$orderLog->log("dangle",array("order"=>$ext,"money"=>$money,"result"=>$result,"serverresult"=>"success"));
				}
			} else
			{	//支付失败
				//$orderNo+"支付失败！"
				//echo "  order:$ext  transaction error ";
				$orderLog->log("dangle","  order:$ext  transaction error ");
				$ret="failed";
			}
			 
		} else {
			//"$sig验证失败"
			//echo "  order:$ext  sign error ";
			$orderLog->log("dangle","  order:$ext  sign error ");
			$ret="failed";
		}
		return $ret;
	}
	
}